const logger = require("../utils/logger")
const mysqlUtil = require("../utils/mysqlUtil")
const commonModule = require('./common')
const moment = require('moment')
const config = require("../config")
const util = require("../utils/util")

module.exports = {
    // 入职申请
    entryApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        logger.debug('entry apply', params)

        params.ruzhiDate = moment(params.ruzhiDate).format('YYYY-MM-DD')

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {
            let res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
            if (res.length == 0) return callback({code: -1, message: '用户不存在'})
            let name = res[0].name

            let sql = 'INSERT INTO tb_entry_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                name: params.name,
                gonghao: params.gonghao,
                department: params.department,
                position: params.position,
                upper: params.upper || '',
                ruzhiDate: params.ruzhiDate,
                phone: params.phone,
                email: params.email,
                gender: params.gender,
                leixing: params.leixing,
                xingzhi: params.xingzhi,
                remark: params.remark || '',
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 3,
                relevenceId: applyId,
                pos: 0,
                title: '入职申请',
                content: name + '的入职申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')
            util.sendReminderSmsToUser(params.auditors[0].uid, '入职申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 撤销入职申请
    revertEntryApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_entry_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 3 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 入职申请记录
    entryApplyRecord: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_entry_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_entry_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_entry_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            for (let i = 0; i != res.length; i++) {
                res[i].ruzhiDate = moment(res[i].ruzhiDate).format('YYYY-MM-DD')
            }
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 入职申请记录web端使用
    entryApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_entry_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_entry_apply WHERE uid = ? ORDER BY id DESC LIMIT ?, ?'
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            for (let i = 0; i != res.length; i++) {
                res[i].ruzhiDate = moment(res[i].ruzhiDate).format('YYYY-MM-DD')
            }
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    entryApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        if (!params.id) return callback({code: -1, message: '参数错误'})

        logger.debug('entry apply detai', params)

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_entry_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 转正申请
    zhuanzhengApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        logger.debug('zhuanzheng apply', params)

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {
            let sql = 'INSERT INTO tb_user_zhuanzheng_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                name: params.name,
                ruzhiDate: params.ruzhiDate,
                shiyongqi: params.shiyongqi,
                department: params.department,
                departmentId: params.departmentId,
                position: params.position,
                positionId: params.positionId,
                upper: params.upper || '',
                upperId: params.upperId,
                zhuanzhengUser: params.zhuanzhengUser,
                zhuanzhengUserName: params.zhuanzhengUserName,
                zhuanzhengDate: params.zhuanzhengDate,
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            let res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 4,
                relevenceId: applyId,
                pos: 0,
                title: '转正申请',
                content: params.zhuanzhengUserName + '的转正申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')
            util.sendReminderSmsToUser(params.auditors[0].uid, '转正申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 撤销转正申请
    revertZhuanzhengApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_user_zhuanzheng_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 4 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 转正申请记录
    zhuanzhengApplyRecord: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_zhuanzheng_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_zhuanzheng_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_zhuanzheng_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            for (let i = 0; i != res.length; i++) {
                res[i].ruzhiDate = moment(res[i].ruzhiDate).format('YYYY-MM-DD')
            }
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 转正申请记录web端使用
    zhuanzhengApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);


        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_zhuanzheng_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = `
                SELECT 
                    tb_user_zhuanzheng_apply.*,
                    tb_user.name upperN,
                    con_department.name departmentN,
                    con_position.name positionN
                FROM tb_user_zhuanzheng_apply 
                INNER JOIN tb_user ON tb_user.uid = tb_user_zhuanzheng_apply.upperId
                INNER JOIN con_department ON con_department.id = tb_user_zhuanzheng_apply.departmentId
                INNER JOIN con_position ON con_position.id = tb_user_zhuanzheng_apply.positionId
                WHERE tb_user_zhuanzheng_apply.uid = ? ORDER BY tb_user_zhuanzheng_apply.id DESC LIMIT ?, ?
            `
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            for (let i = 0; i != res.length; i++) {
                res[i].ruzhiDate = moment(res[i].ruzhiDate).format('YYYY-MM-DD')
            }
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    zhuanzhengApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_user_zhuanzheng_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 离职申请
    departureApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {
            let sql = 'INSERT INTO tb_user_departure_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                name: params.name || '',
                lizhiDate: params.lizhiDate,
                jobDesc: params.jobDesc,
                itemDesc: params.itemDesc,
                receiver: params.receiver,
                receiverName: params.receiverName,
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            let res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 5,
                relevenceId: applyId,
                pos: 0,
                title: '离职申请',
                content: params.name + '的离职申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')
            util.sendReminderSmsToUser(params.auditors[0].uid, '离职申请', '待审批')

            if (params.receiver) {
                // 交接人发个通知
                await mysqlUtil.execute('INSERT INTO tb_user_notification SET ?', [{
                    uid: params.receiver,
                    typ: 5,
                    title: '离职交接',
                    content: params.name + '的离职工作交接给你，请做好交接工作',
                    sta: 0,
                    resTyp: 3,
                    relevenceId: applyId,
                    createTime: Date.now()
                }])
                io.in(params.receiver + '-app').emit('NOTIFICATION-CHANGE')
                io.in(params.receiver + '-app').emit('NOTIFICATION-NEW')
                io.in(params.receiver + '-web').emit('NOTIFICATION-CHANGE')
                io.in(params.receiver + '-web').emit('NOTIFICATION-NEW')
            }

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 撤销离职申请
    revertDepartureApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_user_departure_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 5 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 离职申请记录
    departureApplyRecord: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_departure_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_departure_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_departure_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            for (let i = 0; i != res.length; i++) {
                res[i].ruzhiDate = moment(res[i].ruzhiDate).format('YYYY-MM-DD')
            }
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 离职申请记录web端使用
    departureApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_departure_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = `
                SELECT 
                    tb_user_departure_apply.*,
                    tb_user.name receiverN
                FROM tb_user_departure_apply 
                INNER JOIN tb_user ON tb_user.uid = tb_user_departure_apply.receiver
                WHERE tb_user_departure_apply.uid = ? ORDER BY tb_user_departure_apply.id DESC LIMIT ?, ?
            `
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            for (let i = 0; i != res.length; i++) {
                res[i].ruzhiDate = moment(res[i].ruzhiDate).format('YYYY-MM-DD')
            }
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    departureApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_user_departure_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 转正申请记录
    gongziRecord: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_gongzi WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_gongzi WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_gongzi WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            for (let i = 0; i != res.length; i++) {
                res[i].ruzhiDate = moment(res[i].ruzhiDate).format('YYYY-MM-DD')
            }
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    gongziRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        if (!params.id) return callback({code: -1, message: '参数缺失'})
        let id = parseInt(params.id)

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_gongzi WHERE uploadId = ?', [id])
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_user_gongzi WHERE uploadId = ? LIMIT ?, ?'
            let params = [id, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    gongziTemplateDownloadUrl: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc

        if (!uid) return callback({code: 9999, message: '请重新登录'})


        callback({code: 0, message: 'success', data: config.baseUrl + 'XXXX年XX月工资单.xlsx'})
    },

    newGongziUpload: async (io, socket, params, callback) => {
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        if (!params.month) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('SELECT 1 FROM tb_gongzi_upload WHERE month = ? LIMIT 1', [params.month])
            if (res.length != 0) return callback({code: -1, message: '已存在'})

            await mysqlUtil.execute('INSERT INTO tb_gongzi_upload SET ?', [{month: params.month, totalCnt: 0, totalAmount: 0, createTime: Date.now()}])
            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    gongziUploadRecord: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);


        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_gongzi_upload')
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_gongzi_upload LIMIT ?, ?'
            let params = [(pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

}